package com.lottery.ruok.lottrey.activity;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.dialog.AwardAniDialog;
import com.lottery.ruok.lottrey.dialog.RedPacketAniDialog;
import com.lottery.ruok.lottrey.dialog.SelectRedPacketDialog;
import com.lottery.ruok.lottrey.eventbusObject.ActivitiesCenterRefreshEvent;
import com.lottery.ruok.lottrey.ext.GlideExt;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.ReceiveRewardRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.ActivityListResponse;
import com.lottery.ruok.lottrey.retrofitModule.response.ReceiveRewardResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import com.lottery.ruok.lottrey.utils.ConstantUtil;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ui.IToast;

public class ActivitiesDialogActivity extends FragmentActivity {

    @BindView(R.id.top_bg)
    View topBg;
    @BindView(R.id.iv_activities_icon)
    ImageView ivActivitiesIcon;
    @BindView(R.id.view)
    View view;
    @BindView(R.id.get_reward)
    Button getReward;
    @BindView(R.id.ll_descriptions)
    LinearLayout llDescriptions;
    @BindView(R.id.tv_tip)
    TextView tvTip;
    private ActivityListResponse.ListBean.MissionsBean mData;
    private String mActivitiesId;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_fragment_activities);
        ButterKnife.bind(this);
        setFinishOnTouchOutside(false);
        getData();
        initView();
    }

    private void getData() {
        mData = (ActivityListResponse.ListBean.MissionsBean) getIntent().getSerializableExtra("data");
        mActivitiesId = getIntent().getStringExtra("activities_id");
    }

    private void initView() {
        switch (mData.getStatus()) {
            case ConstantUtil.NO_STARTED:
                getReward.setVisibility(View.GONE);
                tvTip.setText(getResources().getString(R.string.activity_dialog_tip1));
                break;
            case ConstantUtil.PROCESSING:
                getReward.setVisibility(View.GONE);
                tvTip.setText(getResources().getString(R.string.activity_dialog_tip1));
                break;
            case ConstantUtil.PENDIENT_REVIEW:
                getReward.setVisibility(View.GONE);
                tvTip.setText(getResources().getString(R.string.activity_dialog_tip1));
                break;
            case ConstantUtil.READY_TO_RECEIVE_THE_REWARDS:
                getReward.setVisibility(View.VISIBLE);
                tvTip.setText(getResources().getString(R.string.activity_dialog_tip2));
                break;
            case ConstantUtil.COMPLETED:
                getReward.setVisibility(View.GONE);
                tvTip.setText(getResources().getString(R.string.activity_dialog_tip3));
                tvTip.setTextColor(Color.parseColor("#9c9c9c"));
                break;
        }
        if (mData != null) {
            GlideExt.setBackgroundDrawable(this, mData.getModal().getHeader(), topBg, R.mipmap.activities_top);
            GlideExt.setBackgroundDrawable(this, mData.getModal().getButton(), getReward, R.drawable.seek_tops);
            GlideExt.setBackgroundDrawable(this, mData.getIcon().getImage(), ivActivitiesIcon, R.mipmap.activitybet);
            for (int i = 0; i < mData.getDetail().size(); i++) {
                TextView textView = new TextView(this);
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(14f);
                textView.setText(mData.getDetail().size() != 1 ? (i + 1) + "." + mData.getDetail().get(i).getDescription() : mData.getDetail().get(i).getDescription());
                if (mData.getDetail().size() == 1) {
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    params.topMargin = 30;
                    params.gravity = Gravity.CENTER;
                    textView.setLayoutParams(params);
                }
                llDescriptions.addView(textView);
            }
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            this.finish();
            overridePendingTransition(0, R.anim.activity_dialog_out);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @OnClick({R.id.get_reward, R.id.ib_close})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.get_reward:
//                getRewarded();
//                openAwardAnimation();
                openRedPacket();
                break;
            case R.id.ib_close:
                finish();
                overridePendingTransition(0, R.anim.activity_dialog_out);
                break;
        }
    }

    private void openRedPackageAnimation() {
        Intent intent = new Intent(this, RedPacketAniDialog.class);
        intent.putExtra("flag", "flag");
        this.startActivity(intent);
        finish();
    }

    private void getRewarded() {
        RetrofitBase.getRetrofit().create(ReceiveRewardRequest.class)
                .getResult(UserInfo.getToken(), mData.getId(), mData.getLevel())
                .compose(RxHelper.<ReceiveRewardResponse>handleResult())
                .subscribe(new RxSubscribe<ReceiveRewardResponse>(this) {
                    @Override
                    protected void _onNext(ReceiveRewardResponse receiveRewardResponse) {
                        getTip();
                        //这个是给首页发送的事件
                        EventBus.getDefault().post(receiveRewardResponse);
                        //这个是给活动中心发送的事件
                        EventBus.getDefault().post(new ActivitiesCenterRefreshEvent());
                        tvTip.setText(getResources().getString(R.string.activity_dialog_tip3));
                        tvTip.setTextColor(Color.parseColor("#9c9c9c"));
                        getReward.setVisibility(View.GONE);
                    }

                    @Override
                    protected boolean showDialog() {
                        return true;
                    }
                });
    }

    //领取提示
    private void getTip() {
        boolean hasRedPackage = false;
        boolean hasMoney = false;
        for (ActivityListResponse.ListBean.MissionsBean.DetailBean detailBean : mData.getDetail()) {
            if (detailBean.getReward_id() == 4) {
                hasRedPackage = true;
            }
            if (detailBean.getReward_id() == 1) {
                hasMoney = true;
            }
        }
        if (hasRedPackage) {
            openRedPackageAnimation();
        } else {
            if (hasMoney) {
                openAwardAnimation();
            } else {
                IToast.showAlert(this);
            }
        }
    }

    private void openAwardAnimation() {
        Intent intent = new Intent(this, AwardAniDialog.class);
        intent.putExtra("flag", "flag");
        intent.putExtra("money", getMoney());
        this.startActivity(intent);
        finish();
    }

    private void openRedPacket() {
        Intent intent = new Intent(this, SelectRedPacketDialog.class);
        startActivity(intent);
        finish();
    }

    private String getMoney() {
        String money = "";
        for (ActivityListResponse.ListBean.MissionsBean.DetailBean detailBean : mData.getDetail()) {
            if (detailBean.getReward_id() == 1) {
                money = detailBean.getDescription().substring(2, detailBean.getDescription().length() - 1);
            }
        }
        return money;
    }
}
